<?php

namespace App\controller;

use support\Redis;
use support\Request;
use think\facade\Db;
use Illuminate\Support\Env;
use app\controller\RestBase;
use PhpAmqpLib\Wire\AMQPTable;
use App\validate\BatchDropValidate;
use App\validate\BatchBoxDropValidate;
use PhpAmqpLib\Message\AMQPMessage;
use crmeb\exceptions\AdminException;
use think\exception\ValidateException;
use App\service\nft\BatchDropServices;
use App\service\box\BatchBoxServices;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use think\db\exception\{DataNotFoundException, DbException, ModelNotFoundException};


class BlackHoleApi
{
    public function __construct()
    {
        header("Access-Control-Allow-Origin: *");
        //如果需要设置允许所有域名发起的跨域请求，可以使用通配符 * ，如果限制自己的域名的话写自己的域名就行了。
        // 响应类型 *代表通配符，可以指出POST,GET等固定类型
        header('Access-Control-Allow-Methods:*');
        // 响应头设置
        header('Access-Control-Allow-Headers:x-requested-with,content-type');
    }

    /**
     * 批量空投藏品到黑洞账号
     * @param Request $request
     * @return \support\Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function dropSubmit(Request $request)
    {
        $data = [
            'goods_id' => $request->post('goods_id') ?? '', // 选择的要空投的藏品id
            'num' => $request->post('num') ?? '', // 选择的要空投的藏品数量
            'desc' => $request->post('desc') ?? '', // 定向空投 人员
            'date1' => $request->post('date1') ?? '', // 空投时间 年月日
            'date2' => $request->post('date2') ?? '', // 空投时间 时分秒
        ];

        if ($data['desc'] != '') {
            $data['desc'] = explode("\n", $data['desc']);
        }

        $validate = new BatchDropValidate;

        if (!$validate->check($data)) {
            return json(['code' => 0, 'msg' => $validate->getError()]);
        }

        $batchDropServices = new BatchDropServices;

        try {
            $reduce_count = $batchDropServices->blackHoleDrop($data);
        } catch (AdminException $e) {
            return json(['code' => 0, 'msg' => $e->getMessage()]);
        }

        return json(['code' => 1, 'msg' => '提交成功，数量：' . $reduce_count]);
    }

    /**
     * 批量空投盲盒到黑洞账号
     * @param Request $request
     * @return \support\Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function dropBoxSubmit(Request $request)
    {
        $data = [
            'box_id' => $request->post('box_id') ?? '', // 选择的要空投的盲盒id
            'num' => $request->post('num') ?? '', // 选择的要空投的盲盒数量
            'desc' => $request->post('desc') ?? '', // 定向空投 人员
            'date1' => $request->post('date1') ?? '', // 空投时间 年月日
            'date2' => $request->post('date2') ?? '', // 空投时间 时分秒
        ];

        if ($data['desc'] != '') {
            $data['desc'] = explode("\n", $data['desc']);
        }

        $validate = new BatchBoxDropValidate;

        if (!$validate->check($data)) {
            return json(['code' => 0, 'msg' => $validate->getError()]);
        }

        $batchBoxServices = new BatchBoxServices();

        try {
            $batchBoxServices->blackHoleBoxDropNew($data);
        } catch (AdminException $e) {
            return json(['code' => 0, 'msg' => $e->getMessage()]);
        }

        return json(['code' => 1, 'msg' => '提交成功']);
//        return json(['code' => 1, 'msg' => '提交成功，数量：' . $data['num']]);
    }
}
